#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>

#define DSIZE 20
#define OSIZE 10
#define PSIZE 50
//--------------------- **** /Structures/ **** ---------------------------------
//------------------------------------------------------------------------------

   typedef struct
   {
     int day;
     int month;
     int year;
   }DATEOFBIRTH;

	typedef struct PET
	{
		char name[50];
		char species[50];
		DATEOFBIRTH birthDate;
	}pet;

	typedef struct USER
   {
		int idNumber;
		char password[32];
		int type;
   }user;

	typedef struct OWNER
   {
		int idNumber;
		char name[80];
		char address[100];
		char emailAddress[100];
   }owner;

	typedef struct VISIT
   {
		char date[10];
		char serviceType[30];
		char additionalComments[125];
		double payment;
		//payment
	}visit;

	typedef struct DOCTOR
	{
		int id_num;
		char name[70];
		char address[70];
		char gender;
		char specialization[35];
		char degreeEarned[40];
		char dayoff[70];
      int charges;
		//and their current charges
	}doctor;
//---------------------- **** Function Prototypes **** -------------------------
//------------------------------------------------------------------------------
void doctor_function(void);
void owner_function(void);
void pet_function(void);
void doctor_visit(void);
//void charge_offer(void);
void password_function(void);
void report_function(void);
void customer_menu(void);
void manager_menu(void);
void menuChoice(void);

//--------------------- **** /Global Variables/ **** ---------------------------
//------------------------------------------------------------------------------
   FILE *docptr;
   FILE *petptr;
   FILE *ownerptr;
   int choice,pos, index;
   doctor doc_arr[DSIZE]={{{0}},0,"","",' ',"","",""};
//------------------------ **** / Main / **** ----------------------------------
//------------------------------------------------------------------------------
void main()

{
	printf("Welcome to Vetmed Hostipal.\n\n");
   menuChoice();
getch();
}

//-------------------- **** /Function Definitions / **** -----------------------
//------------------------------------------------------------------------------
void menuChoice()
	{

      system("color 30");
		printf("\nEnter the account you wish to login to:\n\n");
		printf("Manager................... 1\n\n");
   	printf("Customer.................. 2\n\n");
		printf("Exit...................... 3\n\n");
   	scanf("%d",&choice);
//------------------------------------------------------------------------------
   	while(choice<1 || choice>3)
   			{
            getchar();
    			printf("\nInvalid option. please enter the correct choice from [1-3]\n");
    			menuChoice();
   			}
//------------------------------------------------------------------------------
  	 	switch(choice)
   			{				 //Starts case
    				case 1: 	manager_menu();
    			           	break;
               case 2:  customer_menu();
                        break;
    				case 3: 	exit(0);
    			          	break;
   			}     		//Ends case

}

/*********************************************************************************************/
/*--------------------------------------------------------------------------------------------*/
void manager_menu(void)
		{
         int manmenu_choice;

         system("cls");
			printf("Welcome to the manager's menu.\n\nPlease select from one of the actions below\n\n");
			printf("Change password.................................... 1\n\n");
			printf("Add/Delete/View doctor information................. 2\n\n");
			printf("Add/Delete/View PetOwner information............... 3\n\n");
			printf("Add/Delete/View Pet information.................... 4\n\n");
			printf("Generate Report.................................... 5\n\n");
			printf("Main Menu.......................................... 6\n\n");
         scanf("%d",&manmenu_choice);

         switch(manmenu_choice)
         {
         		case 1:
               			password_function();
         					break;
               case 2:
               			doctor_function();
               			break;
               case 3:
                        owner_function();
               			break;
               case 4:
                       	pet_function();
                       	break;
               case 5:
                        report_function();
               			break;
               case 6:
                        manager_menu();
               			break;

               default:
               while(manmenu_choice<1 || manmenu_choice>6)
               		{
                      system("cls");
                      	printf("Invalid selection.\n\n");
                      	printf("\nPlease enter a selection from the list below\n");
								printf("Change password.................................. 1\n\n");
								printf("Add/Delete/View doctor information................. 2\n\n");
								printf("Add/Delete/View PetOwner information............... 3\n\n");
								printf("Add/Delete/View Pet information.................... 4\n\n");
								printf("Generate Report.................................... 5\n\n");
								printf("Mainmenu........................................... 6\n\n");
                     	scanf("%d",&manmenu_choice);
                     }

         }     //Ends manmenu case
		}
/**********************************************************************************************/
/*--------------------------------------------------------------------------------------------*/
void customer_menu()
         {
         system("cls");
			printf("Welcome to the Custome's menu.\n\nPlease select from one of the actions below\n\n");
			printf("Visit doctor.................................... 1\n\n");
			printf("View Charges offered............................ 2\n\n");
			printf("Main Menu....................................... 3\n\n");
         printf("Enter choice here:\t");
         scanf("%d", &choice);

         while (choice!=1 || choice!=2 || choice!=3)
         			{
          				system("cls");
			            printf("\n\nInvalid option!!! Please select an option from the list provided\n\n");
                     printf("Visit doctor.................................... 1\n\n");
		            	printf("View Charges offered............................ 2\n\n");
			            printf("Main Menu....................................... 3\n\n");
                     printf("Enter choice here:\t");
                     scanf("%d", &choice);
                     break;
         			}

         switch (choice)
         	{
            	case 1:  doctor_visit();
               			break;
  //          	case 2:  charge_offer();
    //           			break;
            	case 3:  menuChoice();
                       	break;

         	}

         }
/**********************************************************************************************/
/*--------------------------------------------------------------------------------------------*/
void doctor_function()
     {
       FILE *docptr;
       int d_resp ;

  //------------------------------------------------------------------------------

            //Starts Do
            system("cls");

            if ((  docptr=fopen("doctors.dat","wb")) == NULL)
            {
              printf("Error retreiving file");
              manager_menu();
            }
            else
            {
            do{
            printf("Please enter the doctor's name in this format|[firstname] [Lastname]|\n");
            fflush(stdin);
            gets(doc_arr[index].name);

            printf("\nid number:\n");
            scanf("%d", &doc_arr[index].id_num);

            printf("\nAddress:\n");
            fflush(stdin);
            gets(doc_arr[index].address);

            printf("\nGender: [m/f]\n");
            fflush(stdin);
            scanf("%c",&doc_arr[index].gender);

            printf("\nSpecialization:\n");
            fflush(stdin);
            gets(doc_arr[index].specialization);

            printf("\nDegree:\n");
            fflush(stdin);
            gets(doc_arr[index].degreeEarned);

            printf("\nDayoff:\n");
            fflush(stdin);
            gets(doc_arr[index].dayoff);
            system("cls");

fwrite (&doc_arr[index], sizeof(doctor) , 1, docptr);

            printf("\nAdd another doctor record?\nNo...[1]\nYes....[2]\nEnter Choice here:\t");
            scanf("%d", &d_resp);



            ++index;  // continues to the other position in the doctors array

           }while(d_resp != 1);

         fclose(docptr);
        }    //end of else

     ////////////////////////////////////////////////////////////////////////////////////////
	for(pos=0; pos<index;pos++)
     	{

               printf("\n\nThe information below for Doctor %s have been saved\n", doc_arr[pos].name);


               printf("\n Name: %s",doc_arr[pos].name);
               printf("\n ID number: %d",doc_arr[pos].id_num);
               printf("\n Address: %s",doc_arr[pos].address);
               printf("\n Specialization: %s",doc_arr[pos].specialization);
               printf("\n Degree Earned: %s", doc_arr[pos].degreeEarned);
               printf("\n Day<s> Off: %s", doc_arr[pos].dayoff);
	printf("\n------------------------------------------------------------------------\n");
	}
               printf("\n\n total # of record<s> added: %d", index);

               printf("\n\n\n=================================\n");


               printf("\nReturn to main manger's menu...[1]");
               printf("\nReturn to main menu............[2]");
               printf("\n\nSelect next option here:\t");
               fflush(stdin);
               scanf("%d", &choice);

               switch(choice)
               {

                case 1:  manager_menu();
                         break;
                case 2:  menuChoice();
                         break;
                default: printf("\nIncorrect Option. Please Select again:");
                         printf("\nReturn to main manger's menu...[1]");
                         printf("\nReturn to main menu............[2]");
                         printf("\nSelect next option here:\t");
                         fflush(stdin);
                         scanf("%d", &choice);

               }    //end case for choice

              }	  //Ends while loop for fread
/**********************************************************************************************/
/*--------------------------------------------------------------------------------------------*/
void owner_function()
      {
           FILE *ownerptr;
           int col=0;
           int resp ;

           owner own_arr[OSIZE]={0,"","",""};
           //------------------------------------------------------------------------------

            //Starts Do
            system("cls");

            if ((  ownerptr=fopen("petowner.dat","wb")) == NULL)
            {
              printf("Error retreiving file");
              manager_menu();
            }

            else

            		{	//start else
            do{
               printf("\nEnter Pet Owner's identification number: \t");
               fflush(stdin);
               scanf("%d", &own_arr[index].idNumber);

               printf("\nPlease enter the Pet owner's name <[firstname] [Lastname]>\n");
               fflush(stdin);
               gets(own_arr[index].name);

               printf("\nAddress:\n");
               fflush(stdin);
               gets(own_arr[index].address);

               printf("\nEmailAdress:\n");
               fflush(stdin);
               gets(own_arr[index].emailAddress);
               system("cls");
               fwrite (&own_arr[index], sizeof(owner) , 1, ownerptr);

               printf("\nAdd another pet owner record?\nNo...[1]\nYes....[2]\nEnter Choice here:\t");
               scanf("%d", &resp);
            ++index;  // continues to the other position in the doctors array

              }while( resp !=1);
              fclose (ownerptr);
          }//of of else
 ///////////////////////////////////////////////////////////////////////
     for (pos=0 ; pos < index ; pos ++)
     {
         printf("\n\nThe information below for Doctor %s have been saved\n", own_arr[pos].name);
         printf("\n Pet Owner's Name: %s",own_arr[pos].name);
         printf("\n Pet Owner's ID number: %d",own_arr[pos].idNumber);
         printf("\n Pet Owner's Address: %s",own_arr[pos].address);
         printf("\nPet Owner's EmailAddress: %s",own_arr[pos].emailAddress);
	printf("\n------------------------------------------------------------------------\n");
	}
        printf("\n\n total # of record<s> added: %d", index);

        printf("\n\n\n=================================\n");


        printf("\nReturn to main manger's menu...[1]");
        printf("\nReturn to main menu............[2]");
        printf("\n\nSelect next option here:\t");
        fflush(stdin);
        scanf("%d", &choice);

               switch(choice)
               {

                case 1:  manager_menu();
                         break;
                case 2:  menuChoice();
                         break;
                default: printf("\nIncorrect Option. Please Select again:");
                         printf("\nReturn to main manger's menu...[1]");
                         printf("\nReturn to main menu............[2]");
                         printf("\nSelect next option here:\t");
                         fflush(stdin);
                         scanf("%d", &choice);

               }    //end case for choice

      }//end of function
/**********************************************************************************************/
/*--------------------------------------------------------------------------------------------*/
void pet_function()
      {                      
              FILE *petptr;
              int col;
              int p_resp ;

           pet pet_arr[PSIZE]={"","",{0,0,0}};
           //------------------------------------------------------------------------------

            //Starts Do
            system("cls");
            if ((  petptr=fopen("petinfo.dat","wb")) == NULL)
            {
              printf("Error retreiving file");
              manager_menu();
            }
            else
            {
            do{
               printf("\nEnter Pet Name: \t");
               fflush(stdin);
               scanf("%d", &pet_arr[col].name);

               printf("\nPlease enter the Pet's sepecies type:\n");
               fflush(stdin);
               gets(pet_arr[col].species);

               printf("\nEnter pet's birthdate <dd/mm/yyy>:\n");
               fflush(stdin);
               scanf("%d / %d / %d", &pet_arr[col].birthDate.day, &pet_arr[col].birthDate.month, &pet_arr[col].birthDate.year);

               system("cls");

               fflush(stdin);
               printf("\nAdd another pet owner record?\nNo...[1]\nYes....[2]\nEnter Choice here:\t");
               scanf("%d", &p_resp);


            fwrite (&pet_arr, sizeof(pet_arr) , 1, petptr);    //writing the information to the file pet
            ++index;  // continues to the other position in the doctors array

              }while( p_resp !=1);
              fclose (petptr);       //close the pet file
          }//of of else
//////////////////////////////////////////////////////////////////////////////////////
       while(p_resp==1)
      {
      if((ownerptr = fopen("petinfo.dat", "rb")) == NULL)
       {
   	printf("Error. File could not be opened");
      system("pause");
   	manager_menu();
       }else
            {
              while (fread(&pet_arr, sizeof (pet_arr), 1, petptr))
              {
               system("cls");


               printf("\n\nThe information below for %s have been saved\n", pet_arr[col].name);


               printf("\nPet Name: %s",pet_arr[col].name);
               printf("\n Species: %s",pet_arr[col].species);
               printf("\nPet's date of birth: %d/%s/%d",pet_arr[col].birthDate.day, pet_arr[col].birthDate.month, pet_arr[col].birthDate.year);
               printf("\n\n total # of record<s> added: %d", index);

               printf("\n\n\n=================================\n");


               printf("\nReturn to main manger's menu...[1]");
               printf("\nReturn to main menu............[2]");
               printf("\n\nSelect next option here:\t");
               fflush(stdin);
               scanf("%d", &choice);

               switch(choice)
               {

                case 1:  manager_menu();
                         break;
                case 2:  menuChoice();
                         break;
                default: printf("\nIncorrect Option. Please Select again:");
                         printf("\nReturn to main manger's menu...[1]");
                         printf("\nReturn to main menu............[2]");
                         printf("\nSelect next option here:\t");
                         fflush(stdin);
                         scanf("%d", &choice);

               }    //end case for choice

              }	  //Ends while loop for fread

            }       //Ends else


      } //closes 1st while

	}	//Closes that function
/**********************************************************************************************/
/*--------------------------------------------------------------------------------------------*/
void doctor_visit(void)
{

}
/**********************************************************************************************/
/*---------------------------------------------------------------------------------------------*/
/*void charge_offer(void)
{
 		system("cls");

      while (!feof)      //looping the action by checking for the end of the file
    			{             //Starts while loop

            //using the fread function to read from the file
      		fread(&doctor, sizeof(doctor),1,docptr);

            //Printing the information to the cutomers displaying some of the doctor's information
      		printf("\nDoctor's name: %s\nGender: %c\nSpecialization: %s \ncharges: %d \n",own_arr[col].name,own_arr[col].gender,own_arr[col].specialization,own_arr[col].charges);
    			}//Close the while loop

            printf("\nPress any key to continue");
            getch();   //freezes the screen to accept input so it can continue to customer menu

            customer_menu();





} */
/**********************************************************************************************/
/*---------------------------------------------------------------------------------------------*/
void report_function(void)
{
       system("cls");
       pos=0;

       docptr = fopen("doctors.dat", "rb");
       if(docptr  == NULL)
       {
         printf("\nError. File could not be found");
         system("pause");
         manager_menu();
       }
	else

   		{   //starts else
           	while (!feof(docptr))
           		{   //starts while
                    // prints doctor name
                    	fread(&doc_arr, sizeof(doctor),1,docptr);
                     printf("\n%s", doc_arr[index].name);

                     index++;
           		}   //ends while
              fclose(docptr); 

         }   //ends else
    } 

/**********************************************************************************************/
/*--------------------------------------------------------------------------------------------*/
void password_function(void)
{

}
